package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.redpacket.RedPacketInfo;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

import static com.zy.openchat.Constants.PAGE_COUNT;


public class RedPacketRecordViewModel extends PagedViewModel<MultiItemViewModel, RedPacketInfo> {
    private static final String MULTIRECYCLETYPE_TOP = "top";
    private static final String MULTIRECYCLETYPE_MIDDLE = "middle";
    private int recordType;

    public void setRecordType(int recordType) {
        this.recordType = recordType;
    }

    public RedPacketRecordViewModel(@NonNull Application application) {
        super(application);
        if (observableList.size() == 0) {
            MultiItemViewModel item = new RedPacketRecordTopViewModel(this, "0", "0.0",  recordType);
            item.multiItemType(MULTIRECYCLETYPE_TOP);
            observableList.add(item);
        }
    }

    @Override
    public void onMutilItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
        super.onMutilItemBind(itemBinding, position, item);
        //通过item的类型, 动态设置Item加载的布局
        String itemType = (String) item.getItemType();
        if (MULTIRECYCLETYPE_TOP.equals(itemType)) {
            //设置头布局
            itemBinding.set(BR.viewModel, R.layout.item_multi_redpacket_record_top);
        } else if (MULTIRECYCLETYPE_MIDDLE.equals(itemType)) {
            //设置中间布局
            itemBinding.set(BR.viewModel, R.layout.item_multi_redpacket_record_middle);
        }
    }

    public void requestData(int page) {

        TelegramManager.getTelegramManager().queryRedHistoryCall(getApplication(), recordType == 1 ? 2 : 1, PAGE_COUNT, currentPage, new TgResultBlock<List<RedPacketInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<RedPacketInfo> obj) {
                finishRefreshAndLoad(page, obj, MultiItemViewModel.class);

            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                finishRefreshAndLoad(page, null, MultiItemViewModel.class);
            }
        });

    }

    @Override
    public void addData(List<RedPacketInfo> objects, Class<MultiItemViewModel> t) {
        if (observableList.size() == 0) {

            float totalPrice = 0.00f;
            for (RedPacketInfo itemsEntity : objects) {
                if (recordType == 1) {
                    totalPrice += itemsEntity.curUserRpPrice();
                } else if (recordType == 2) {
                    totalPrice += itemsEntity.total_price;
                }
            }
            MultiItemViewModel item = new RedPacketRecordTopViewModel(this, Integer.toString(objects.size()),Float.toString(totalPrice),  recordType);
            item.multiItemType(MULTIRECYCLETYPE_TOP);
            observableList.add(item);
        }
        if (objects != null && objects.size() > 0) {
            addDatas(objects);
        }
    }

    @Override
    public int initContentView() {
        return 0;
    }

    private void addDatas(List<RedPacketInfo> objects) {

        for (RedPacketInfo itemsEntity : objects) {
            //双向绑定动态添加Item
            MultiItemViewModel item = new RedPacketRecordMiddleViewModel(this, itemsEntity, recordType);
            item.multiItemType(MULTIRECYCLETYPE_MIDDLE);
            observableList.add(item);
        }
    }

    @Override
    public boolean showEmptyWrap() {
        return true;
    }
}
